<!-- 新建或编辑岗位 -->
<template>
  <div class='create_job_post'>
    <dt-modal-edit ref='jobPost' :title='title' :label-width='100' okText='确定' :width='80'
      @ok='onOk'>
      <Tabs ref="targetTabs" type="card" v-model="curState" :animated="false"
        name='jobPostManagement'>
        <TabPane label="岗位信息" name="jobManage" tab='jobPostManagement'>
          <dt-info ref='jobManage' :labelWidth='150' :options='optionsjobManage' :info="info">
          </dt-info>
        </TabPane>
        <TabPane label="对应角色" name="role" tab='jobPostManagement'>
          <corres-role ref="corresRole"></corres-role>
        </TabPane>
        <TabPane label="对应职级" name="Rank" tab='jobPostManagement'>
          <corres-rank ref="corresRank"></corres-rank>
        </TabPane>
      </Tabs>
    </dt-modal-edit>
  </div>
</template>

<script>
  import DtInfo from '@/components/dt/info/dt-info.vue'
  import editMgr from '../opt/edit'
  import corresRole from './corresRole.vue'
  import corresRank from './corresRank.vue'
  import jobApi from '@/biz/postManagement'
  export default {
    components: { DtInfo, corresRole, corresRank },
    data() {
      return {
        title: '',
        curState: 'jobManage',
        optionsjobManage: [],
        rolelist: [],
        ranklist: [],
        info: {},
        flag: 'create',
      }
    },
    methods: {
      getRole() {
        let params = {
          id: this.info.id ? this.info.id : null,
        }
        jobApi.getRoleAndPosition(params).then((res) => {
          this.$refs.corresRole.getData(res.roleList)
          this.$refs.corresRank.getData(res.dtRankList)
          this.rolelist = res.roleList
          this.rankList = res.dtRankList
        })
      },
      create(data) {
        this.title = data.title
        this.flag = 'create'
        this.info = {
          positionName: '',
          bonusFlag: '1',
          stateFlag: '1',
          remark: '',
        }
        this.show()
      },
      edit(data) {
        this.flag = 'edit'
        this.title = data.title
        data.bonusFlag = String(data.bonusFlag)
        data.stateFlag = String(data.stateFlag)
        this.info = data
        this.show()
      },
      show() {
        this.$refs.jobPost.show()
        this.optionsjobManage = editMgr.optionsjobManage()
        this.curState = 'jobManage'
        this.getRole()
      },
      hide() {
        this.$refs.jobPost.hide()
      },
      onOk() {
        this.$refs.jobManage.validate().then((res) => {
          if (res) {
            let roles = this.$refs.corresRole.roleId.join(',')
            let dtRankList = this.$refs.corresRank.tableList
            let rankOrder = []
            dtRankList && dtRankList.forEach((item, index) => {
              item.rankIds = []
              item.rankId = ''
              item.positionRankVOList.forEach(itemL => {
                item.rankIds.push(itemL.rankId)
                rankOrder.push(itemL.rankId)
              })
              item.sort = parseInt(index) + 1
              item.rankId = item.rankIds.length > 0 ? item.rankIds.join(',') : ''

            });
            let params = {
              ...this.info,
              roles,
              dtRankList,
              rankOrder: rankOrder.join(','),
              delDtRankIds: this.$refs.corresRank.delDtRankIds
            }
            if (this.flag == 'edit') {
              params.updateId = this.$dt.userInfo().userId
              jobApi.updatePosition(params).then((res) => {
                this.$emit('success')
                this.hide()
              })
            } else {
              jobApi.savePosition(params).then((res) => {
                this.$emit('success')
                this.hide()
              })
            }
          } else {
            this.curState = 'jobManage'
          }
        })
      },
    },
  }
</script>
<style lang='scss' scoped>
</style>